<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="80px"
      class="formList"
    >
      <el-row>
        <!-- <el-col :span="5"> -->
          <el-form-item label="供应商" prop="supplierUid">
            <el-select
              v-model="queryParams.supplierUid"
              placeholder="请选择供应商"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in supplierUidOptions"
                :key="dict.supplierUid"
                :label="dict.supplierName"
                :value="dict.supplierUid"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="证书类型" prop="certificateType">
            <el-select
              v-model="queryParams.certificateType"
              placeholder="请选择证书类型"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in certificateTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="证件名称" prop="certificateName">
            <el-input
              v-model="queryParams.certificateName"
              placeholder="请输入证件名称"
              clearable
              size="small"
              @keyup.enter.native="handleQuery"
            />
          </el-form-item>
        <!-- </el-col> -->

      </el-row>
      <el-row>
        <!-- <el-col :span="5"> -->
          <el-form-item label="数据状态" prop="dataStatus">
            <el-select
              v-model="queryParams.dataStatus"
              placeholder="请选择数据状态"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in dataStatusOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col>
        <el-col :span="5"> -->
          <el-form-item label="对外展示" prop="showUser">
            <el-select
              v-model="queryParams.showUser"
              placeholder="请选择对外展示"
              clearable
              size="small"
            >
              <el-option
                v-for="dict in showUserOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        <!-- </el-col> -->

        <!-- <el-col :span="4"> -->
          <el-form-item style="margin-left: 10px">
            <el-button
              type="primary"
              icon="el-icon-search"
              size="mini"
              @click="handleQuery"
            >搜索
            </el-button
            >
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
            >重置
            </el-button
            >
          </el-form-item>
        <!-- </el-col> -->
      </el-row>

    </el-form>
    <el-row class="row-mbinfo" style="margin-left: 0px; margin-right: 0px;">
      <el-row >
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasPermi="['supplier:certificate:add']"
          >新增
          </el-button
          >
        </el-col>
        <el-col :span="1.5" style="margin-left: 10px">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['supplier:certificate:edit']"
          >修改
          </el-button
          >
        </el-col>
        <el-col :span="1.5" style="margin-left: 10px">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['supplier:certificate:remove']"
          >删除
          </el-button
          >
        </el-col>
        <el-col :span="1.5" style="margin-left: 10px">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['supplier:certificate:export']"
          >导出
          </el-button
          >
        </el-col>
        <right-toolbar
          :showSearch.sync="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>
    </el-row>
    <div class="divTable">
      <el-table
        border
        v-loading="loading"
        :data="certificateList"
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center"/>

        <el-table-column
          label="证件"
          align="center"
          prop="certificateUrl"
          :show-overflow-tooltip="true"
          sortable
        >
          <template slot-scope="scope">
            <el-image v-if="scope.row.certificateUrl != null && scope.row.certificateUrl != '' " slot="reference"
                      :src="scope.row.certificateUrl"
                      class="image-rows" :preview-src-list="[scope.row.certificateUrl]"></el-image>
          </template>
        </el-table-column>
        <el-table-column
          label="供应商名称"
          align="center"
          prop="supplierName"
          :show-overflow-tooltip="true"
          sortable
          min-width="120"
        />
        <el-table-column
          label="证件名称"
          align="center"
          prop="certificateName"
          :show-overflow-tooltip="true"
          sortable
        />
        <el-table-column
          label="证书类型"
          align="center"
          prop="certificateType"
          :formatter="certificateTypeFormat"
          sortable
        />
        <el-table-column label="对外展示" align="center" prop="showUser">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.showUser"
              active-value="1"
              inactive-value="2"
              @change="handleShowStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="dataStatus">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.dataStatus"
              active-value="1"
              inactive-value="0"
              @change="handleStatusChange(scope.row)"
            ></el-switch>
          </template>
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
          class-name="small-padding fixed-width"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-zoom-in"
              @click="handleShowDetail(scope.row)"
              v-hasPermi="['supplier:certificate:query']"
            >查看
            </el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['supplier:certificate:edit']"
            >修改
            </el-button
            >
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-if="scope.row.dataStatus == '0'"
              v-hasPermi="['supplier:certificate:remove']"
            >删除
            </el-button
            >
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </div>

    <!-- 添加或修改供应商资质对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="open"
      width="500px"
      append-to-body
      @keyup.enter.native="submitForm"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="供应商" prop="supplierUid">
          <el-select style="width: 100%" v-model="form.supplierUid" placeholder="请选择供应商">
            <el-option
              v-for="dict in supplierUidOptions"
              :key="dict.supplierUid"
              :label="dict.supplierName"
              :value="dict.supplierUid"
            />
          </el-select>
        </el-form-item>
          <el-form-item label="证件名称" style="width: 100%" prop="certificateName">
            <el-input
              v-model="form.certificateName"
              placeholder="请输入证件名称"
            />
          </el-form-item>
          <el-form-item label="证书类型" prop="certificateType">
            <el-select
              style="width: 100%"
              v-model="form.certificateType"
              placeholder="请选择证书类型"
            >
              <el-option
                v-for="dict in certificateTypeOptions"
                :key="dict.dictValue"
                :label="dict.dictLabel"
                :value="dict.dictValue"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="证件图片" prop="certificateUrl">
            <imageUpload v-model="form.certificateUrl"/>
          </el-form-item>
          <el-form-item label="对外展示" prop="showUser">
            <el-radio-group v-model="form.showUser">
              <el-radio
                v-for="dict in showUserOptions"
                :key="dict.dictValue"
                :label="dict.dictValue"
              >{{ dict.dictLabel }}
              </el-radio
              >
            </el-radio-group>
          </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 查看供应商资质对话框 -->
    <el-dialog
      :title="viewTitle"
      :visible.sync="viewOpen"
      width="700px"
      append-to-body
    >
      <el-form ref="detailForm">
        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>承诺信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item>
                <el-form-item label="证书名称">
                  <span>{{ showData.certificateName }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

            <el-col :span="12">
              <el-form-item>
                <el-form-item label="证件类型">
                  <span>{{ showDataCertificateType }}</span>
                </el-form-item>
              </el-form-item>
            </el-col>

          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="证件图片">
                <el-image
                  :src="showData.certificateUrl"
                  style="width: 100px; height: 100px"
                  :z-index="zIndex"
                  :preview-src-list="[showData.certificateUrl]"
                ></el-image>
              </el-form-item>
            </el-col>
          </el-row>
        </div>

        <div>
          <i class="MmsUiBlockTitle___line___27lhp1-44-1"></i>
          <div class="MmsUiBlockTitle___title___2C-H-1-44-1">
            <span>其他信息</span>
          </div>
        </div>
        <el-divider></el-divider>
        <div style="margin-left: 50px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="更新时间">
                <span>{{ showData.updateTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="更新人账号">
                <span>{{ showData.updateName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item label="创建时间">
                <span>{{ showData.createTime }}</span>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="创建人账号">
                <span>{{ showData.createName }}</span>
              </el-form-item>
            </el-col>
          </el-row>

        </div>


      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="mini" @click="handCloseDetail"
        >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import {
    listCertificate,
    getCertificate,
    delCertificate,
    addCertificate,
    updateCertificate,
    updateDataStatus,
    updateShowStatus,
  } from "@/api/supplier/certificate";
  import ImageUpload from "@/components/ImageUpload";
  import {getSupplierList} from '@/api/supplier/info'

  export default {
    name: "Certificate",
    components: {
      ImageUpload,
    },
    data() {
      return {
        supplierUidOptions:[],
        //证书类型翻译
        showDataCertificateType: '',
        //图片预览
        zIndex: 3000,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 供应商资质表格数据
        certificateList: [],
        // 弹出层标题
        title: "",
        //查看详细标题
        viewTitle: "",
        // 是否显示弹出层
        open: false,
        //是否显示查看详细弹出层
        viewOpen: false,
        //选中数组的名字
        names: [],
        //查看详细数据
        showData: [],
        // 证书类型字典
        certificateTypeOptions: [],
        // 数据状态字典
        dataStatusOptions: [],
        // 对外展示字典
        showUserOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          certificateType: null,
          certificateName: null,
          dataStatus: null,
          showUser: null,
        },
        columns: [
          {
            label: "修改时间",
            dataIndex: "certificateId",
          },
          {
            label: "平台ID",
            dataIndex: "platformUid",
          },
          {
            label: "供应商ID",
            dataIndex: "supplierUid",
          },
          {
            label: "证书类型",
            dataIndex: "certificateType",
          },
          {
            label: "证件名称",
            dataIndex: "certificateName",
          },
          {
            label: "证件地址",
            dataIndex: "certificateUrl",
          },
          {
            label: "数据状态",
            dataIndex: "dataStatus",
          },
          {
            label: "对外展示",
            dataIndex: "showUser",
          },
          {
            label: "创建人账号",
            dataIndex: "createName",
          },
          {
            label: "创建人",
            dataIndex: "createUser",
          },
          {
            label: "创建时间",
            dataIndex: "createTime",
          },
          {
            label: "修改人",
            dataIndex: "updateUser",
          },
          {
            label: "修改人账号",
            dataIndex: "updateName",
          },
          {
            label: "修改时间",
            dataIndex: "updateTime",
          },
        ],
        // 表单参数
        form: {},
        // 表单校验
        rules: {
          supplierUid:[
            {required: true, message: "请选择供应商", trigger: "change"},
          ],
          certificateType: [
            {required: true, message: "证书类型不能为空", trigger: "change"},
          ],
          certificateName: [
            {required: true, message: "证件名称不能为空", trigger: "blur"},
            { min: 1, max: 15, message: '证件名称长度在 1 到 15 个字符', trigger: 'blur' }
          ],
          certificateUrl: [
            {required: true, message: "证件地址不能为空", trigger: "blur"},
          ],
          showUser: [
            {required: true, message: "对外展示不能为空", trigger: "blur"},
          ],
        },
      };
    },
    created() {
      this.getList();
      this.getDicts("supplier_certificate_type").then((response) => {
        this.certificateTypeOptions = response.data;
      });
      this.getDicts("sys_data_status").then((response) => {
        this.dataStatusOptions = response.data;
      });
      this.getDicts("supplier_qualification_show").then((response) => {
        this.showUserOptions = response.data;
      });
    },
    //生命周期 - 挂载完成（可以访问DOM元素）
    mounted() {
      getSupplierList(1).then((response) => {
        this.supplierUidOptions = response.data
      })
    },
    methods: {
      /** 查询供应商资质列表 */
      getList() {
        this.loading = true;
        listCertificate(this.queryParams).then((response) => {
          this.certificateList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 证书类型字典翻译
      certificateTypeFormat(row, column) {
        return this.selectDictLabel(
          this.certificateTypeOptions,
          row.certificateType
        );
      },
      // 数据状态字典翻译
      dataStatusFormat(row, column) {
        return this.selectDictLabel(this.dataStatusOptions, row.dataStatus);
      },
      // 对外展示字典翻译
      showUserFormat(row, column) {
        return this.selectDictLabel(this.showUserOptions, row.showUser);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          certificateId: null,
          platformUid: null,
          supplierUid: null,
          certificateType: null,
          certificateName: null,
          certificateUrl: null,
          dataStatus: "0",
          showUser: "1",
          createName: null,
          createUser: null,
          createTime: null,
          updateUser: null,
          updateName: null,
          updateTime: null,
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map((item) => item.certificateId);
        this.names = selection.map((item) => item.certificateName);
        this.single = selection.length !== 1;
        this.multiple = !selection.length;
        //用于禁用删除
        if (this.multiple == false) {
          for (var i = 0; i < selection.length; i++) {
            var element = selection[i];
            if ("1" === element.dataStatus) {
              this.multiple = true;
              break;
            }
          }
        }
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "添加供应商资质";
      },
      /** 查看详情按钮操作 **/
      handleShowDetail(row) {
        this.viewOpen = true;
        this.viewTitle = "查看供应商资质";
        let this_ = this;
        this.showData = row;
        this.certificateTypeOptions.forEach(function (value, key) {
          if (value.dictValue == row.certificateType) {
            this_.showDataCertificateType = value.dictLabel
            return false
          }
        })


      },
      handCloseDetail() {
        this.viewOpen = false;
        this.showData = [];
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const certificateId = row.certificateId || this.ids;
        getCertificate(certificateId).then((response) => {
          this.form = response.data;
          this.open = true;
          this.title = "修改供应商资质";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate((valid) => {
          if (valid) {
            if (this.form.certificateId != null) {
              updateCertificate(this.form).then((response) => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addCertificate(this.form).then((response) => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const certificateIds = row.certificateId || this.ids;
        const name = row.certificateName || this.names;
        this.$confirm(
          '是否确认删除证件名称为"' + name + '"的数据项?',
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return delCertificate(certificateIds);
          })
          .then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download(
          "supplier/certificate/export",
          {
            ...this.queryParams,
          },
          `supplier_certificate.xlsx`
        );
      },
      // 状态修改
      handleStatusChange(row) {
        let text = row.dataStatus === "0" ? "无效" : "有效";
        this.$confirm(
          '确定要把"' + row.certificateName + '"资质,设为' + text + "吗？",
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return updateDataStatus(row.certificateId, row.dataStatus,row.supplierUid);
          })
          .then(() => {
            this.msgSuccess("操作成功");
            this.getList();
          })
          .catch(function () {
            row.dataStatus = row.dataStatus === "0" ? "1" : "0";
          });
      },

      // 状态显示修改
      handleShowStatusChange(row) {
        let text = row.dataStatus === "0" ? "不显示" : "显示";
        this.$confirm(
          '确定要把"' + row.certificateName + '"资质,设为' + text + "吗？",
          "警告",
          {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          }
        )
          .then(function () {
            return updateShowStatus(row.certificateId, row.showUser,row.supplierUid);
          })
          .then(() => {
            this.msgSuccess("操作成功");
            this.getList();
          })
          .catch(function () {
            row.dataStatus = row.dataStatus === "0" ? "1" : "0";
          });
      },
    },
  };
</script>
<style scoped>
  .MmsUiBlockTitle___line___27lhp1-44-1 {
    display: inline-block;
    background: #407cff;
    width: 2px;
    height: 16px;
    margin-right: 8px;
  }

  .MmsUiBlockTitle___title___2C-H-1-44-1 {
    display: inline-block;
    font-size: 20px;
    color: #333;
    position: relative;
  }
</style>
